package com.irdstudio.efp.flow.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.efp.flow.service.dao.InstFlowEventDao;
import com.irdstudio.efp.flow.service.domain.InstFlowEvent;
import com.irdstudio.efp.flow.service.facade.InstFlowEventService;
import com.irdstudio.efp.flow.service.vo.InstFlowEventVO;
import com.irdstudio.basic.framework.core.base.FrameworkService;

/**
 * 
 * 
 * 
 * @author Cytus_
 * @since 2018年5月8日 下午7:48:02
 * @version 1.0
 *
 */
@Service("instFlowEventService")
public class InstFlowEventServiceImpl implements InstFlowEventService, FrameworkService {

	
	private static Logger logger = LoggerFactory.getLogger(InstFlowEventServiceImpl.class);
	
	@Autowired
	private InstFlowEventDao instFlowEventDao;
	
	
	public int updateNodeAndEventStatue(InstFlowEventVO eventVo) {
		
		logger.debug("根据业务流水号:"+ eventVo.getBizSerno() +"更新状态："+ eventVo.getBizEventState() +"开始......");
		int num = 0;
		InstFlowEvent event = beanCopy(eventVo, new InstFlowEvent());
		try {
			num = this.instFlowEventDao.updateNodeAndEventStatue(event);
			logger.debug("根据业务流水号:"+ eventVo.getBizSerno() +"更新状态："+ eventVo.getBizEventState() +"结束......");
		} catch (Exception e) {
			logger.error("根据业务流水号:"+ eventVo.getBizSerno() +"更新状态："+ eventVo.getBizEventState() +"出现异常!", e);
			num = -1;
		}
		return num;
	}


	@Override
	public int insertInstFlowEvent(InstFlowEventVO instFlowEvent) {
		logger.debug("新增数据开始......");
		int num = 0;
		InstFlowEvent event = beanCopy(instFlowEvent, new InstFlowEvent());
		try {
			num = this.instFlowEventDao.insertInstFlowEvent(event);
			logger.debug("新增数据结束......");
		} catch (Exception e) {
			logger.error("新增数据出现异常!", e);
			num = -1;
		}
		return num;
	}


	@Override
	public InstFlowEventVO queryByBizSerno(String bizSerno) {
		logger.debug("根据业务流水号:"+ bizSerno +"查询信息开始......");
		InstFlowEventVO eventVo = null;
		try {
			InstFlowEvent event = this.instFlowEventDao.queryByBizSerno(bizSerno);
			eventVo = beanCopy(event, new InstFlowEventVO());
			logger.debug("根据业务流水号:"+ bizSerno +"查询信息结束......");
		} catch (Exception e) {
			logger.error("根据业务流水号:"+ bizSerno +"查询信息出现异常!", e);
		}
		return eventVo;
	}
	
	public InstFlowEventVO queryByNodeIdAndEventId(InstFlowEventVO vo) {
		InstFlowEventVO instFlowEventVO = null;
		try {
			logger.debug("根据节点实例号："+ vo.getInstNodeId() +"和事件编号:"+ vo.getBizEventId() +"查询信息开始......");
			InstFlowEvent queryevent = beanCopy(vo, new InstFlowEvent());
			InstFlowEvent event = instFlowEventDao.queryByNodeIdAndEventId(queryevent);
			instFlowEventVO = beanCopy(event, new InstFlowEventVO());
			logger.debug("根据节点实例号："+ vo.getInstNodeId() +"和事件编号:"+ vo.getBizEventId() +"查询信息结束......");
		} catch (Exception e) {
			logger.error("根据节点实例号："+ vo.getInstNodeId() +"和事件编号:"+ vo.getBizEventId() +"查询信息出现异常!", e);
		}
		return instFlowEventVO;
	}


	@Override
	public int updateInstNodeIdByBizSerno(String bizSerno, String instNodeId) {
		logger.debug("根据事件实例号："+ bizSerno +"更新节点实例号"+ instNodeId +"开始......");
		InstFlowEvent event = new InstFlowEvent();
		event.setBizSerno(bizSerno);
		event.setInstNodeId(instNodeId);
		int num = 0;
		try {
			num = instFlowEventDao.updateInstNodeIdByBizSerno(event);
			logger.debug("根据事件实例号："+ bizSerno +"更新节点实例号"+ instNodeId +"结束......");
		} catch (Exception e) {
			logger.debug("根据事件实例号："+ bizSerno +"更新节点实例号"+ instNodeId +"出现异常!", e);
			num = -1;
		}
		return num;
	}

}
